//
//  main.swift
//  PerfectTemplate
//
//  Created by Kyle Jessup on 2015-11-05.
//	Copyright (C) 2015 PerfectlySoft, Inc.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//

import PerfectHTTP
import PerfectHTTPServer

// An example request handler.
// This 'handler' function can be referenced directly in the configuration below.
func handler(request: HTTPRequest, response: HTTPResponse) {
	// Respond with a simple message.
	response.setHeader(.contentType, value: "application/json, text/plain")
    do {
        try response.setBody(json: "{\"status\":\"1\",\"info\":null,\"time\":1533615638,\"data\":[{\"postId\":123453,\"images\":\"/group1/M00/01/F1/rBLt4Fta3ZSAJUGxAAB46Z92-KI170.jpg\",\"time\":1532681982,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":122915,\"images\":\"/group1/M00/01/F1/rBLt4Fta3yyAB01XAAC6CjCH-7w214.jpg\",\"time\":1532076105,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":122627,\"images\":\"/group1/M00/01/D7/rBLt4FtRPoCAUMQuAADazhBTvP4016.jpg\",\"time\":1531734938,\"url\":\"http://m.imoduo.com/#/activity/share\",\"type\":2,\"activityId\":\"61\"},{\"postId\":122593,\"images\":\"/group1/M00/01/CD/rBLt4FtMD7yAVYb9AADZ2TUwYTs238.jpg\",\"time\":1531711429,\"url\":\"http://m.imoduo.com/#/activity2/share\",\"type\":2,\"activityId\":\"59\"},{\"postId\":122516,\"images\":\"/group1/M00/01/D9/rBLt4FtRpoaAJIzpAAD12W9t4Iw365.jpg\",\"time\":1531473124,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":122187,\"images\":\"/group1/M00/01/CA/rBLt4FtIbYyAV0utAACqa40eef8230.jpg\",\"time\":1530868952,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":121816,\"images\":\"/group1/M00/01/BE/rBLt4Fs_NQqAR4FtAADuKVmEslU636.jpg\",\"time\":1530264157,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":121423,\"images\":\"/group1/M00/01/B0/rBLt4Fs1-pWAJXHaAAC7PqBuTcY216.jpg\",\"time\":1529654791,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":121048,\"images\":\"/group1/M00/01/9F/rBLt4FssuzmAeACnAAEHTG_EdN8341.jpg\",\"time\":1529045433,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":120483,\"images\":\"/group1/M00/01/97/rBLt4FsqIweAcELDAAB_SPRCEhY427.jpg\",\"time\":1528446028,\"url\":\"\",\"type\":1,\"activityId\":\"\"}]}")
    } catch {
        //...
    }
//    response.appendBody(string: "{\"status\":\"1\",\"info\":null,\"time\":1533615638,\"data\":[{\"postId\":123453,\"images\":\"/group1/M00/01/F1/rBLt4Fta3ZSAJUGxAAB46Z92-KI170.jpg\",\"time\":1532681982,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":122915,\"images\":\"/group1/M00/01/F1/rBLt4Fta3yyAB01XAAC6CjCH-7w214.jpg\",\"time\":1532076105,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":122627,\"images\":\"/group1/M00/01/D7/rBLt4FtRPoCAUMQuAADazhBTvP4016.jpg\",\"time\":1531734938,\"url\":\"http://m.imoduo.com/#/activity/share\",\"type\":2,\"activityId\":\"61\"},{\"postId\":122593,\"images\":\"/group1/M00/01/CD/rBLt4FtMD7yAVYb9AADZ2TUwYTs238.jpg\",\"time\":1531711429,\"url\":\"http://m.imoduo.com/#/activity2/share\",\"type\":2,\"activityId\":\"59\"},{\"postId\":122516,\"images\":\"/group1/M00/01/D9/rBLt4FtRpoaAJIzpAAD12W9t4Iw365.jpg\",\"time\":1531473124,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":122187,\"images\":\"/group1/M00/01/CA/rBLt4FtIbYyAV0utAACqa40eef8230.jpg\",\"time\":1530868952,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":121816,\"images\":\"/group1/M00/01/BE/rBLt4Fs_NQqAR4FtAADuKVmEslU636.jpg\",\"time\":1530264157,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":121423,\"images\":\"/group1/M00/01/B0/rBLt4Fs1-pWAJXHaAAC7PqBuTcY216.jpg\",\"time\":1529654791,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":121048,\"images\":\"/group1/M00/01/9F/rBLt4FssuzmAeACnAAEHTG_EdN8341.jpg\",\"time\":1529045433,\"url\":\"\",\"type\":1,\"activityId\":\"\"},{\"postId\":120483,\"images\":\"/group1/M00/01/97/rBLt4FsqIweAcELDAAB_SPRCEhY427.jpg\",\"time\":1528446028,\"url\":\"\",\"type\":1,\"activityId\":\"\"}]}")
    
    let cookie = HTTPCookie(name: "xiaoming", value: "haha", domain: nil,
                            expires: .session, path: "/",
                            secure: false, httpOnly: false)
    response.addCookie(cookie)
	// Ensure that response.completed() is called when your processing is done.
	response.completed()
    let foos: [String] = request.params(named: "foo")
    print("request is", cookie)
}

// Configuration data for an example server.
// This example configuration shows how to launch a server
// using a configuration dictionary.


let confData = [
	"servers": [
		// Configuration data for one server which:
		//	* Serves the hello world message at <host>:<port>/
		//	* Serves static files out of the "./webroot"
		//		directory (which must be located in the current working directory).
		//	* Performs content compression on outgoing data when appropriate.
		[
			"name":"localhost",
			"port":8181,
			"routes":[
				["method":"get", "uri":"/index", "handler":handler],
				["method":"get", "uri":"/file", "handler":PerfectHTTPServer.HTTPHandler.staticFiles,
				 "documentRoot":"./webroot",
				 "allowResponseFilters":true]
			],
			"filters":[
				[
				"type":"response",
				"priority":"high",
				"name":PerfectHTTPServer.HTTPFilter.contentCompression,
				]
			]
		]
	]
]

do {
	// Launch the servers based on the configuration data.
	try HTTPServer.launch(configurationData: confData)
} catch {
	fatalError("\(error)") // fatal error launching one of the servers
}

